// 个人信用评分
apiready = function(){
        //初始化窗体
      	iniFrom();
      	$api.html($api.byId("mobile"),api.pageParam.mobile);
      	queryScore(api.pageParam.mobile);
}


 function iniFrom(){
        api.readFile({
            path: 'widget://creditScore/creditScore.json'
      	}, function(ret, err){
            var data=$api.strToJson(JSON.stringify(ret)).data;
            data=$api.strToJson(data).array;
            var contentHtml="";
            for(var i=0;i<data.length;i++){
              	contentHtml=contentHtml+"<div class='nav-li'><div class='tit' >"+data[i].typeLable+"</div><div class='subItem'><table >";
              	var values=data[i].values;
                for(var k=0;k<values.length;k++){
                  	var items=values[k].items
	              	for(var j=0;j<items.length;j++){
	                    if(j===0){
	                      contentHtml=contentHtml+'<tr><td  rowspan='+items.length+' width=20%>'+values[k].sign +
	                      '</td><td width=60%>'+items[j].name+'</td><td width=10%>'+items[j].value +
	                      '</td><td width=10%><input id="'+data[i].id+'-'+ values[k].id+'-'+ items[j].id+'" name="'+values[k].groupSign+'" class=\'radio\' onclick=\'calAndSaveScore()\'  type="radio" value='+ items[j].value+' /></td></tr>';
	                    }
	                    else{
	                      contentHtml=contentHtml+'<tr><td>'+items[j].name+'</td><td >'+items[j].value+
	                      '</td><td ><input id="'+data[i].id+'-'+ values[k].id+'-'+ items[j].id+'" name="'+values[k].groupSign+'" class=\'radio\' onclick=\'calAndSaveScore()\'    type="radio" value='+ items[j].value+' /></td></tr>';
	                    }
	                }
	                contentHtml=contentHtml+"  <tr><td></td><td></td><td></td><td></td></tr>";
	          	}
	        	contentHtml=contentHtml+"  </table></div></div>";
      		}
	      	if(contentHtml){
	        	$api.byId('content').innerHTML=contentHtml;
	      	}
  		});
}
// 评分查询
function queryScore(mobile){
  $api.html($api.byId("sign"),"手机号码："+mobile);
  // 提交JSON数据
  api.ajax({
    url: 'http://'+sourceIp+':20099/scoreInfo/select?m='+mobile,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    }
  }, function(ret, err) {
    if (ret) {
      if(ret.data[0]){
        var ids=ret.data[0].scoreInfo.split(';');
        for(var i=0;i<ids.length;i++){
          $api.attr($api.byId(ids[i]),'checked','checked');
        }
        $api.html($api.byId("score"), ret.data[0].score+"分");
      }
    } else {
        alert(JSON.stringify(err));
    }
  });
}



// 评分计算并保存
function calAndSaveScore(){
  var   mobile = $api.byId("mobile").innerHTML;
  var   inputList = $api.domAll(".radio");
  var score=0;
  var ids="";
  for (var i = 0, length = inputList.length; i < length; i++) {
    if(inputList[i].checked){
      score=score+parseInt(inputList[i].value);
      ids=ids+inputList[i].id+";";
    }
  }
  if(score ){
    // 提交JSON数据
    api.ajax({
      url: 'http://'+sourceIp+':20099/scoreInfo/save',
      method: 'post',
      headers: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      data: {
        body: {
          mobile: mobile,
          scoreInfo: ids,
          score: score
        }
      }
    });
    $api.html($api.byId("score"), score+"分");
	} else {
      $api.toast( '请选择',5000);}
}
